<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Repository\Smilapp\SmilappNoticeRepository;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use App\Repository\AppUser\TouTiaoRepository;
use App\Repository\AppUser\TouTiaoUserOrderRepository;
use App\Repository\UploadRepository;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class FileUploadController extends BaseController
{
  public function __construct()
  {
    $this->orderKeysTime = 10;
  }
  public function fileUpload(Request $request)
  {
    $params = $request->all();
    // 先判断用户是否存在
    $resUser = DB::Table('toutiao_user')->where(['shop_id' => $params['shopId'], 'id' => $params['code']])->first();
    if (!$resUser) {
      return $this->returnMsg(100, '请您登录后再提建议');
    }
    // var_dump($params);
    if (empty($resUser->updated_at)) {
      return $this->returnMsg(101, '请您退出刷新后重新进入');
    }
    if (!empty($resUser->updated_at) &&  strtotime($resUser->updated_at) > (time() - 600)) {
      return $this->returnMsg(101, '请您退出刷新后重新进入');
    }
    // 图片提交限制
    // 5分钟内最多提交10条
    $cacheKey = $params['shopId'] . '_' . $params['code'] . '_repeat';
    $nums = 1;
    $getNums = Cache::get($cacheKey);
    if ($getNums > 10) {
      // return $this->returnMsg(103, '您提交太频繁,请稍后在提交');
    }
    $nums = $getNums + 1;
    Cache::put($cacheKey, $nums, Carbon::now()->addMinutes($this->orderKeysTime));
    // 验证用户是否已经提交过了
    // var_dump($params);
    $model = new UploadRepository();
    $res = $model->upload($request, 'file', true, $params['shopId'], 'yijianfankui');
    // var_dump($res);
    return $this->returnMsg(200, '上传成功', $res);
  }
}
